//
//  XZ_SlideColorController.m
//  Project_Demo
//
//  Created by Leon on 2018/7/26.
//  Copyright © 2018年 caoxiaozhu. All rights reserved.
//

#import "XZ_SlideColorController.h"
#import "XZ_SlideColorView.h"

@interface XZ_SlideColorController ()

@property(nonatomic,strong) XZ_SlideColorView *sliderView;

@end

@implementation XZ_SlideColorController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _sliderView = [[[NSBundle mainBundle]loadNibNamed:@"XZ_SlideColorView" owner:self options:nil]firstObject];
    _sliderView.frame = self.view.bounds;
    [self.view addSubview:_sliderView];
    
  RACSignal *redSingal =   [self slider:_sliderView.redSlider WithTextField:_sliderView.redTextField];
   RACSignal *greenSingal =[self slider:_sliderView.greenSlider WithTextField:_sliderView.greenTextField];
   RACSignal *blueSingal = [self slider:_sliderView.blueSlider WithTextField:_sliderView.blueTextField];
    [[[RACSignal combineLatest:@[redSingal,greenSingal,blueSingal]] map:^id _Nullable(RACTuple * _Nullable value) {
        return [UIColor colorWithRed:[value[0] floatValue] green:[value[1] floatValue] blue:[value[2] floatValue] alpha:1];
    }]subscribeNext:^(id  _Nullable x) {
        self.sliderView.colorImageView.backgroundColor = x;
    }] ;
}

-(RACSignal *)slider:(UISlider *)slider WithTextField:(UITextField *)textField{
    
    RACSignal *textSingal = [[textField rac_textSignal] take:1];
    RACChannelTerminal *sliderSignal = [slider rac_newValueChannelWithNilValue:nil];
    RACChannelTerminal *signalText = [textField rac_newTextChannel];
    [[sliderSignal map:^id _Nullable(id  _Nullable value) {
       return [NSString stringWithFormat:@"%.02f",[value floatValue]];
    }] subscribe:signalText];
    
    [signalText subscribe:sliderSignal];
    
    return [[signalText merge:sliderSignal] merge:textSingal];
}


@end
